﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.TypeList
{
    public partial class List : System.Web.UI.Page
    {
        protected FrameHelper _framePage;


        public string _kind;

        protected void Page_Load(object sender, EventArgs e)
        {
            _kind = String.IsNullOrEmpty(Request["kind"]) ? "" : Request["kind"];
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            if (_framePage.IsAjaxPagerPostBack)
            {
                BindList(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                BindList(1, 20);
            }
        }



        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using(ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                return Convert.ToInt32(session.CreateQuery("select count(*) from CmsTreeDef where Kind=:kind")
                    .SetParameter("kind", _kind).UniqueResult());
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using(ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                gridArticle.DataSource = session.CreateQuery("from CmsTreeDef where Kind=:kind order by Id desc").
                    SetParameter("kind", _kind).
                    SetFirstResult((pageIndex - 1) * pageSize).
                    SetMaxResults(pageSize).List();
            }
            gridArticle.DataBind();
        }

    }
}
